//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using ViNoToolkit;

public class ViNoSceneNodeFactory : ISpriteFactory {

	public override GameObject Create( ref SceneData.SceneNodeData data , GameObject node , Vector3 pos , Vector3 scl , GameObject parent ){
		LoadData( ref data , node );
		return node;
	}
				
	public override void SaveData( ref SceneData.SceneNodeData dat ,  GameObject obj  ){
		base.SaveData( ref dat , obj );
				
		// Serialize Sprite Data .
		MeshRenderer meshRenderer = obj.GetComponent<MeshRenderer>();
		if( meshRenderer  != null ){
			dat.nodeType = SceneNodeType.LEAF;
			dat.uiAtlasName = "";
			dat.alpha = meshRenderer.sharedMaterial.color.a;
			dat.show = meshRenderer.enabled;
		}		
		else{
			ColorPanel p =  obj.GetComponent<ColorPanel>();
			dat.nodeType = SceneNodeType.NODE;
			if( p != null ){			
				dat.alpha = p.alpha;
			}
			else{
				dat.alpha = 1f;				
			}
			dat.show = true;
		}				
		
		ViNoSceneNode vs = obj.GetComponent<ViNoSceneNode>();		
		if( vs != null ){					
			vs.singleNodeData = dat;		
			if( meshRenderer  != null ){
//				vs.scrNodeData.tex = meshRenderer.sharedMaterial.mainTexture as Texture2D;
			}
		}			
	}
	
	public override void LoadData( ref SceneData.SceneNodeData data ,  GameObject obj ){
		if( obj != null ){
			ViNoSceneNode vs = obj.GetComponent<ViNoSceneNode>();
			if( vs != null ){	
				vs.singleNodeData = data;
				vs.Reload( );
			}											
		}
		else{
			Debug.LogWarning( "GameObject :" + data.parentname + "/" + data.name + " isn't found in this scene." );
		}
	}
}